package ch.teamgalaxy.imagepuzzle;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import ch.teamgalaxy.imagepuzzle.TouchImageView;


public class MainActivity extends Activity
{
	
	private TextView txtInfo;
	private Uri outputFileUri;
	private final int INTENT_CAMERA = 0;
	private final int INTENT_CROP = 1;
	private TouchImageView touchView1;
	private RelativeLayout layout;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		layout = (RelativeLayout) findViewById(R.id.linearlayout1);
		txtInfo = (TextView) findViewById(R.id.textView1);

		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.activity_main, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			
			Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			
			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
			
			txtInfo.setText("");

			String path = Environment.getExternalStorageDirectory().getAbsolutePath();
			path += "/puzzleimage_" + timeStamp + ".jpg";
			File file = new File( path );
//			file.mkdirs();
			outputFileUri = Uri.fromFile( file );
			//String absoluteOutputFileUri = file.getAbsolutePath();

			camera.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
			startActivityForResult(camera, INTENT_CAMERA);
			
			return true;
			
		case R.id.delete_button:
			// remove images from screen
			layout.removeAllViews();
			
		
		default:
			return super.onOptionsItemSelected(item);
		}
		
		
		
	}

		
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == INTENT_CAMERA) {
				Uri picUri = outputFileUri;
				cropImage(picUri);
	
			} else if (requestCode == INTENT_CROP) {
				Bundle extras = data.getExtras();
				Bitmap thePic = extras.getParcelable("data");
				
				touchView1 = new TouchImageView(this);
				touchView1.setBitmap(thePic);
				layout.addView(touchView1);
				
				// Delete Image from SD card
				File tempFile = new File(outputFileUri.getPath());
				Boolean isDeleted = tempFile.delete();
				if (!isDeleted) {
					Toast toastNotDeleted = Toast.makeText(this, "The Image hasn't been deleted", Toast.LENGTH_LONG);
					toastNotDeleted.show();
					
					
				}
			}
			
			
 
            
		}
	}
	

	
	
//	@Override
//    public boolean onNavigationItemSelected(int itemPosition, long itemId)
//    {
//		btnStart.setText("Click");
//		
//		
//		return true;
//    }
	void cropImage(Uri ImageUri) {
		
//		Intent crop = new Intent("com.android.camera.action.CROP");
//		crop.setDataAndType(ImageUri, "image/*");
//		crop.putExtra("return-data", true);
//		startActivityForResult(crop, INTENT_CROP);
		
		Intent cropIntent = new Intent("com.android.camera.action.CROP");
	    
		cropIntent.setDataAndType(ImageUri, "image/*");
		   
		cropIntent.putExtra("crop", "true");
		cropIntent.putExtra("aspectX", 1);
		cropIntent.putExtra("aspectY", 1);
		cropIntent.putExtra("outputX", 300);
		cropIntent.putExtra("outputY", 300);
		cropIntent.putExtra("return-data", true);
		startActivityForResult(cropIntent, INTENT_CROP);
			
	}
	

}
